/**
 * 商品中间件
 * @type {Goods}
 ***/
/**************/
const Goods = require('../model/goods');

module.exports = {

  /**
   * 添加商品中间件
   * @param request
   * @param response
   * @param next
   */
  sGoods: (request, response, next) => {
    // console.log('sGoods=====' + request.tokenData.username);
    let {name, describe, price, store, date_name, goods_category_id, index} = request.body;
    let username = request.tokenData.username; // 从token获取username
    let goods = {
      name,
      describe,
      price,
      store,
      src: '/upload/user/' + username + '/' + username + '0' +date_name + '.png', // 与写入的文件的路径保持一致,存入数据库<因为写入文件与此是异步的,所以不能通过写入文件完成以后再发送路径,因为此时已经把数据存入数据库了>
      goods_category_id,
      index
    }

    /**进行写入数据库*/
    Goods.setGoods(goods)
      .then(result => { // 添加商品成功
        request.add_gid = result;
        next();
      })
      .catch(error => { // 添加商品失败
        next(error);
      })
  },

  /**
   * 删除商品
   * @param request
   * @param response
   * @param next
   */
  dGoods: (request, response, next) => {
    let {id} = request.body
    Goods.deleteGoods(id)
      .then(result => {
        request.affectR = result;    //把受影响的行数返回到request中
        next()
      })
      .catch(error => {
        next(error)
      })
  },

  /**
   * 修改商品信息sql
   * @param request
   * @param response
   * @param next
   */
  uGoods: (request, response, next) => {
    // console.log('sGoods=====' + request.tokenData.username);
    let {id, name, describe, price, store, date_name, goods_category_id, index, src} = request.body;
    let username = request.tokenData.username; // 从token获取username
    let goods = {
      id,
      name,
      describe,
      price,
      store,
      src: date_name ? '/upload/user/' + username + '/' + username + '0' +date_name + '.png' : src, // 与写入的文件的路径保持一致,存入数据库<因为写入文件与此是异步的,所以不能通过写入文件完成以后再发送路径,因为此时已经把数据存入数据库了>
      goods_category_id,
      index
    }

    console.log('name=' + goods.name + ';describe=' + goods.describe + ';price=' + goods.price + ';src='
      + goods.src + ';goods_category_id=' + goods.goods_category_id + ';index=' + goods.index + ';id=' + goods.id);

    Goods.upDateGoods(goods)
      .then(result => {
        request.affectR = result
        next();
      })
      .catch(error => {
        next(error);
      })
  },

  /**
   * 获取指定的用户记录条数中间件
   * @param request
   * @param response
   * @param next
   */
  gGoodsCount: (request, response, next) => {
    let {id, goods_category_id, root_category_id, username, name_describe_goodsCategoryName_rootCategoryName} = request.body
    console.log('-------------------------------------root_category_id----------------------------' + root_category_id);

    Goods.getGoodsCount(id, goods_category_id, root_category_id, username, name_describe_goodsCategoryName_rootCategoryName)
      .then(result => {
        request.goodsCount = result
        next()
      })
      .catch(error => {
        next(error)
      })
  },

  /**
   * 获取指定的商品信息中间件
   * @param request
   * @param response
   * @param next
   */
  gGoodsRowCountRecord: (request, response, next) => {
    let start = response.start;
    let size = response.size;

    let {id, goods_category_id, root_category_id, username, name_describe_goodsCategoryName_rootCategoryName} = request.body

    Goods.getGoods(start, size, id, goods_category_id, root_category_id, username, name_describe_goodsCategoryName_rootCategoryName)
      .then(result => {
        request.recordCountGoods = result
        next()
      })
      .catch(error => {
        next(error)
      })
  }


}

